#include "stdafx.h"
#include "glutcontroler.h"

int button_pressed = 0;
int button_type = 0;
int GLUTmouse[2] = { 0, 0 };

void mouse(int button, int state, int x, int y)
{
	  button_pressed = (state == GLUT_DOWN) ? 1 : 0;
	  button_type = button;

	  GLUTmouse[0] = x;
	  GLUTmouse[1] = Glut_h - y;
}

void mousedrag(int x, int y)
{
	  y = Glut_h - y;

	  int dx = x - GLUTmouse[0];
	  int dy = y - GLUTmouse[1];

	  GLUTmouse[0] = x;
	  GLUTmouse[1] = y;

	  if (dx == 0 && dy == 0) return;
	  if (button_pressed == 0) return;

	  if (button_type == GLUT_LEFT_BUTTON )
	  {
		  myVector3D rightVector; crossproduct(scene->camera_up,scene->camera_forward,rightVector);
		  scene->camera_eye = scene->camera_eye + (rightVector * 0.03 * dx);
		  scene->camera_eye = scene->camera_eye + (scene->camera_up * 0.03 * -dy);
	  }
	  else if (button_type == GLUT_RIGHT_BUTTON )
	  {
		  myVector3D rightVector; crossproduct(scene->camera_up,scene->camera_forward,rightVector);rightVector.normalize();
		  myVector3D deplacementVector = (rightVector*dx) + (scene->camera_up*-dy);
		  myVector3D rotateAxis; crossproduct(deplacementVector,scene->camera_forward,rotateAxis);rotateAxis.normalize();
	  
		  rotate2(scene->camera_forward,rotateAxis,0.03);
		  //rotate2(scene->camera_eye,rotateAxis,0.05);
		  //rotate2(scene->camera_up,rotateAxis,0.05);
	  }
  
	  /*cout << "camera_eye : " << scene->camera_eye.X << "," << scene->camera_eye.Y << "," << scene->camera_eye.Z << endl ;
	  cout << "camera_forward : " << scene->camera_forward.dX << "," << scene->camera_forward.dY << "," << scene->camera_forward.dZ << endl ;
	  cout << "camera_up : " << scene->camera_up.dX << "," << scene->camera_up.dY << "," << scene->camera_up.dZ << endl << endl << flush ;*/

	  glutPostRedisplay();
}

void mouseWheel(int button, int dir, int x, int y)
{
    if (dir > 0)
        scene->camera_eye = scene->camera_eye + (scene->camera_forward * 0.5);
    else
        scene->camera_eye = scene->camera_eye - (scene->camera_forward * 0.5);
}

void keyboard(unsigned char key, int x, int y) {
	switch(key) {
	case 27:
		exit(0) ;
        break ;
	case 'n' :
		scene->displayNormal = !scene->displayNormal;
		break;
	case 's' :
		scene->displaySilhouette = !scene->displaySilhouette;
		break;
	case 'l':
		scene->light.mylight_type++;
		scene->light.mylight_type = scene->light.mylight_type % 4;
		break;
	case 'g' :
		scene->displayGooch = !scene->displayGooch;
		break;
	case 't' :
		scene->render_type++;
		scene->render_type = scene->render_type % 3;
		break;
	}
	glutPostRedisplay();
}